import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'

Vue.config.productionTip = false

import '@/styles/element/element.scss'
import ElementUI from 'element-ui'
Vue.use(ElementUI)

import VueClipboard from 'vue-clipboard2'
Vue.use(VueClipboard)

// 自定义组件
import Icon from './components/icon/Icon.vue'
Vue.component('icon', Icon)
import IconSelector from './components/icon/IconSelector.vue'
Vue.component('icon-selector', IconSelector)

// 路由守卫
import { NavigationGuardNext, RouteMeta } from 'vue-router'
router.beforeEach((to: RouteMeta, from: RouteMeta, next: NavigationGuardNext) => {
  if (to.meta.title) {
    document.title = to.meta.title
  }
  if (to.meta.requireAuth) {
    // console.log(store)
    if (! store.getters['cms/is_logged_in']) {
      store.dispatch('cms/getAuth')
    }
    
    if (store.getters['cms/is_logged_in']) {
      next()
    } else {
      next({
        name: 'login',
        query: {
          redirect: to.fullPath
        }
      })
    }
  } else {
    next()
  }
})

// router.afterEach(() => {
// })

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
